


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: AbstractDatabaseDataStore</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.datastore.database</a> ]
</div>

<h1>Coverage Summary for Class: AbstractDatabaseDataStore (at.schauer.gregor.vienna.datastore.database)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">AbstractDatabaseDataStore</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (5/ 5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    92.3%
  </span>
  <span class="absValue">
    (24/ 26)
  </span>
</td>
</tr>
  <tr>
    <td class="name">AbstractDatabaseDataStore$1</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (2/ 2)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    83.3%
  </span>
  <span class="absValue">
    (5/ 6)
  </span>
</td>
  </tr>
<tr>
  <td class="name"><strong>total</strong></td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (7/ 7)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    90.6%
  </span>
  <span class="absValue">
    (29/ 32)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.datastore.database;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContext;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.datastore.DataStore;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.datastore.StorageMetadata;
<i>21</i>&nbsp;import at.schauer.gregor.vienna.serializer.Serializer;
<i>22</i>&nbsp;import org.springframework.jdbc.core.JdbcTemplate;
<i>23</i>&nbsp;import org.springframework.jdbc.support.DatabaseMetaDataCallback;
<i>24</i>&nbsp;import org.springframework.jdbc.support.JdbcUtils;
<i>25</i>&nbsp;import org.springframework.jdbc.support.MetaDataAccessException;
<i>26</i>&nbsp;
<i>27</i>&nbsp;import java.sql.DatabaseMetaData;
<i>28</i>&nbsp;import java.sql.ResultSet;
<i>29</i>&nbsp;import java.sql.SQLException;
<i>30</i>&nbsp;import java.util.HashSet;
<i>31</i>&nbsp;import java.util.Map;
<i>32</i>&nbsp;import java.util.Set;
<i>33</i>&nbsp;
<i>34</i>&nbsp;/**
<i>35</i>&nbsp; * @author Gregor Schauer
<i>36</i>&nbsp; */
<i>37</i>&nbsp;public abstract class AbstractDatabaseDataStore&lt;T, X&gt; implements DataStore&lt;T&gt; {
<i>38</i>&nbsp;	protected Set&lt;String&gt; tables;
<i>39</i>&nbsp;	protected JdbcTemplate template;
<i>40</i>&nbsp;	protected ViennaContext ctx;
<i>41</i>&nbsp;
<b class="fc"><i>42</i>&nbsp;	public AbstractDatabaseDataStore(ViennaContext ctx, JdbcTemplate template) {</b>
<b class="fc"><i>43</i>&nbsp;		this.ctx = ctx;</b>
<b class="fc"><i>44</i>&nbsp;		this.template = template;</b>
<b class="fc"><i>45</i>&nbsp;	}</b>
<i>46</i>&nbsp;
<i>47</i>&nbsp;	@Override
<i>48</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>49</i>&nbsp;	public &lt;E extends T&gt; E load(Class&lt;E&gt; type, Object identifier) {
<b class="fc"><i>50</i>&nbsp;		StorageMetadata&lt;E&gt; metadata = StorageMetadata.create(ctx, type);</b>
<b class="fc"><i>51</i>&nbsp;		metadata.identifier = identifier;</b>
<i>52</i>&nbsp;
<b class="fc"><i>53</i>&nbsp;		X data = read(metadata);</b>
<b class="fc"><i>54</i>&nbsp;		return map(data, metadata);</b>
<i>55</i>&nbsp;	}
<i>56</i>&nbsp;
<i>57</i>&nbsp;	protected abstract &lt;E extends T&gt; X read(StorageMetadata&lt;E&gt; metadata);
<i>58</i>&nbsp;
<i>59</i>&nbsp;	protected abstract &lt;E extends T&gt; E map(X data, StorageMetadata&lt;E&gt; metadata);
<i>60</i>&nbsp;
<i>61</i>&nbsp;	@Override
<i>62</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>63</i>&nbsp;	public Object save(T obj) {
<b class="fc"><i>64</i>&nbsp;		StorageMetadata metadata = StorageMetadata.create(ctx, obj);</b>
<i>65</i>&nbsp;
<b class="fc"><i>66</i>&nbsp;		Serializer&lt;T&gt; serializer = ctx.getSerializer(metadata.type);</b>
<b class="fc"><i>67</i>&nbsp;		Map&lt;String, Object&gt; map = serializer.save(obj);</b>
<b class="fc"><i>68</i>&nbsp;		map.remove(metadata.identifierName);</b>
<i>69</i>&nbsp;
<b class="fc"><i>70</i>&nbsp;		if (metadata.identifier == null) {</b>
<b class="fc"><i>71</i>&nbsp;			metadata.identifier = ctx.getIdentifierGenerator(metadata.type).generate(obj);</b>
<b class="fc"><i>72</i>&nbsp;			metadata.identifier = insert(metadata);</b>
<b class="fc"><i>73</i>&nbsp;			ctx.getMetadataProvider((Class&lt;T&gt;) obj.getClass()).setIdentifier(obj, metadata.identifier);</b>
<b class="fc"><i>74</i>&nbsp;			return metadata.identifier;</b>
<i>75</i>&nbsp;		} else {
<b class="fc"><i>76</i>&nbsp;			update(metadata);</b>
<b class="fc"><i>77</i>&nbsp;			return metadata.identifier;</b>
<i>78</i>&nbsp;		}
<i>79</i>&nbsp;	}
<i>80</i>&nbsp;
<i>81</i>&nbsp;	protected abstract Object insert(StorageMetadata&lt;T&gt; metadata);
<i>82</i>&nbsp;
<i>83</i>&nbsp;	protected abstract void update(StorageMetadata&lt;T&gt; metadata);
<i>84</i>&nbsp;
<i>85</i>&nbsp;	protected Set&lt;String&gt; getTables() {
<b class="fc"><i>86</i>&nbsp;		if (tables == null) {</b>
<i>87</i>&nbsp;			try {
<b class="fc"><i>88</i>&nbsp;				tables = getTablesFromMetadata();</b>
<b class="nc"><i>89</i>&nbsp;			} catch (MetaDataAccessException e) {</b>
<b class="nc"><i>90</i>&nbsp;				throw new RuntimeException(e);</b>
<b class="fc"><i>91</i>&nbsp;			}</b>
<i>92</i>&nbsp;		}
<b class="fc"><i>93</i>&nbsp;		return tables;</b>
<i>94</i>&nbsp;	}
<i>95</i>&nbsp;
<i>96</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>97</i>&nbsp;	protected Set&lt;String&gt; getTablesFromMetadata() throws MetaDataAccessException {
<b class="fc"><i>98</i>&nbsp;		return (Set&lt;String&gt;) JdbcUtils.extractDatabaseMetaData(template.getDataSource(), new DatabaseMetaDataCallback() {</b>
<i>99</i>&nbsp;			@Override
<i>100</i>&nbsp;			public Object processMetaData(DatabaseMetaData dbmd) throws SQLException {
<b class="fc"><i>101</i>&nbsp;				Set&lt;String&gt; names = new HashSet&lt;String&gt;();</b>
<b class="fc"><i>102</i>&nbsp;				ResultSet rs = dbmd.getTables(null, null, &quot;%&quot;, new String[]{&quot;TABLE&quot;});</b>
<b class="fc"><i>103</i>&nbsp;				while (rs.next()) {</b>
<b class="nc"><i>104</i>&nbsp;					names.add(rs.getString(3));</b>
<i>105</i>&nbsp;				}
<b class="fc"><i>106</i>&nbsp;				return names;</b>
<i>107</i>&nbsp;			}
<i>108</i>&nbsp;		});
<i>109</i>&nbsp;	}
<i>110</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:22</div>
</div>
</body>
</html>
